﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SchoolManagementSystem
{
    public partial class MuridAdd : Form
    {
        private Murid parent = null;
        private OrtuFind ortufind = null;
        public int par_id = -1;
        private string imagePath = "";

        public MuridAdd()
        {
            InitializeComponent();
        }

        public MuridAdd(Murid instance)
        {
            InitializeComponent();
            this.parent = instance;

            if (this.parent.dTable == null)
            {
                this.parent.dTable = Database.getInstance().query("select * from Student");
            }

            // Set Maximum stu_id length
            this.txtInduk.MaxLength = 6;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                // display image in picture box
                this.picFoto.Image = new Bitmap(open.FileName);
                this.imagePath = open.FileName;

            }
        }

        private void btnOrtu_Click(object sender, EventArgs e)
        {
            if (this.ortufind == null || this.ortufind.IsDisposed)
                ortufind = new OrtuFind(this);

            //check whether the form has been opened or not
            if (((Main)this.MdiParent).isOpenForm(ortufind))
            {
                ortufind = null;
                return;
            }

            ortufind.MdiParent = this.MdiParent;
            ortufind.Show();
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MuridAdd_Load(object sender, EventArgs e)
        {
            // Load School Session
            DateTime date = DateTime.Now;
            if (date.Month < 7)
            {
                num1.Value = date.Year - 1;
                num2.Value = num1.Value + 1;
            }
        }

        private void btnCek_Click(object sender, EventArgs e)
        {
            if (this.txtInduk.Text == "")
            {
                MessageBox.Show("Anda belum memasukkan nomor induk", "Peringatan !");
                return;
            }

            DataTable dt = Database.getInstance().tes("select * from Student where stu_id='" + this.txtInduk.Text + "'");
            if (dt.Rows.Count == 1)
            {
                MessageBox.Show("Nomor induk ini sudah ada", "Peringatan !");
            }
            else
            {
                MessageBox.Show("Nomor induk tidak ditemukan", "Peringatan !");
            }
        }

        private void getTodayBtn_Click(object sender, EventArgs e)
        {
            regBox.Value = DateTime.Now;
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            if (false == checkFieldData())
            {
                MessageBox.Show("Harap isi semua kolom isian !", "Peringatan !");
                return;
            }

            DataTable dt = Database.getInstance().query("select * from Student");

            // FileStream fs = new FileStream(this.imagePath, FileMode.Open, FileAccess.Read);
            // byte[] picbyte = new byte[fs.Length];
            // fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
            // fs.Close();

            DataRow dataRow = dt.NewRow();
            dataRow["stu_id"] = this.txtInduk.Text;
            dataRow["stu_name"] = this.txtNama.Text;
            dataRow["stu_alias"] = this.txtPanggilan.Text;

            if (this.cmbKelamin.Text == "Laki-laki")
            {
                dataRow["stu_gender"] = "L";
            }
            else
            {
                dataRow["stu_gender"] = "P";
            }

            dataRow["stu_birthdate"] = this.birthdayBox.Value.Date;
            dataRow["stu_birthplace"] = this.txtTmpLahir.Text;
            dataRow["stu_regdate"] = this.regBox.Value;
            dataRow["stu_regsession"] = num1.Value + " - " + num2.Value;
            dataRow["stu_regform"] = this.txtAsal.Text;
            dataRow["stu_desc"] = this.txtStatus.Text;
            dataRow["par_id"] = this.par_id;
            dataRow["stu_image"] = ImageToByteArray(this.picFoto.Image);

            dt.Rows.Add(dataRow);

            Database.getInstance().updateDB(dt);

            var result = MessageBox.Show("Tambah murid berhasil", "Sukses !");
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                this.Close();
            }
        }

        private Boolean checkFieldData()
        {
            if (this.txtInduk.Text == "" ||
                this.txtNama.Text == "" ||
                this.txtPanggilan.Text == "" ||
                this.cmbKelamin.Text == "" || 
                this.birthdayBox.Text == "" || 
                this.txtTmpLahir.Text == "" ||
                this.regBox.Text == "" ||
                this.txtAsal.Text == "" ||
                this.txtStatus.Text == "" ||
                this.txtOrtu.Text == "" ||
                this.txtAlamat.Text == "" ||
                this.txtPekerjaan.Text == "")  
            {
                return false;
            }

            return true;
        }

        private byte[] ImageToByteArray(Image img)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }

        private void txtInduk_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void num1_ValueChanged(object sender, EventArgs e)
        {
            num2.Value = num1.Value + 1;
        }

        private void num2_ValueChanged(object sender, EventArgs e)
        {
            num1.Value = num2.Value - 1;
        }

        private void label11_Click(object sender, EventArgs e)
        {

        }
    }
}